﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAO
{
    public class FriendService
    {
        private String CMD;

        /// <summary>
        ///     获取指定分组中的所有好友信息
        /// </summary>
        /// <param name="ItemId" type="string">
        ///     <para>
        ///         分组Id
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回null , 则表示不存在该分组
        /// </returns>
        public DataTable GetFriendByItemId(String ItemId)
        {
            CMD = "SELECT * FROM TB_FRIEND WHERE Friend_ItemId=" + ItemId;
            SqlDataAdapter Adapter = new SqlDataAdapter(CMD, DB.DB_CONN);
            using(DataSet DS = new DataSet("DS_FRIENDINFO_BY_ITEMID"))
            {
                Adapter.Fill(DS, "TB_FRIENDINFO_BY_ITEMID");
                return DS.Tables["TB_FRIENDINFO_BY_ITEMID"];
            }
        }

        /// <summary>
        ///     添加指定好友到指定分组
        /// </summary>
        /// <param name="ItemId">分组ID</param>
        /// <param name="FriendUserId">好友用户ID</param>
        /// <returns>返回-1 , 则表示添加失败</returns>
        public int AddFriend(int ItemId, int FriendUserId)
        {
            CMD = "INSERT INTO TB_FRIEND(Friend_ItemId, Friend_UserId) VALUES(" + ItemId + "," + FriendUserId + ")";
            using (SqlCommand Command = new SqlCommand(CMD, DB.DB_CONN))
            {
                DB.DB_CONN.Open();
                if (Command.ExecuteNonQuery() > 0)
                {
                    DB.CloseDB();
                    return FriendUserId;
                }
                DB.CloseDB();
                return -1;
            }
        }

        /// <summary>
        ///     通过好友ID得到属于该好友的所有好友的分组ID
        /// </summary>
        /// <param name="FriendId">好友ID</param>
        /// <returns>返回所有分组ID</returns>
        public String[] GetItemsByFriendId(int FriendId)
        {
            String ItemId = "";
            CMD = "SELECT Friend_ItemId FROM TB_FRIEND WHERE Friend_UserId=" + FriendId;
            SqlDataAdapter Adapter = new SqlDataAdapter(CMD, DB.DB_CONN);
            using (DataSet DS = new DataSet("DS_FRIENDINFO_BY_ITEMID"))
            {
                Adapter.Fill(DS, "TB_ITEMINFO_BY_FRIENDID");
                foreach (DataTable dt in DS.Tables)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        ItemId += dr["Friend_ItemId"].ToString();
                    }
                }
            }
            return ItemId.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        }

        /// <summary>
        ///     删除用户中指定分组ID和好友ID
        /// </summary>
        /// <param name="ItemId">分组ID</param>
        /// <param name="FriendName">好友ID</param>
        /// <returns>返回false, 则表示删除好友失败</returns>
        public bool DeleteFriend(int ItemId, int FriendId)
        {
            CMD = "DELETE FROM TB_FRIEND WHERE Friend_ItemId=" + ItemId + " and Friend_UserId=" + FriendId;
            using (SqlCommand Command = new SqlCommand(CMD, DB.DB_CONN))
            {
                DB.DB_CONN.Open();
                if (Command.ExecuteNonQuery() > 0)
                {
                    DB.CloseDB();
                    return true;
                }
                DB.CloseDB();
                return false;
            }
        }


        public bool DeleteFriend(int[] ItemId, int FriendId)
        {
            CMD = "DELETE FROM TB_FRIEND WHERE Friend_UserId=" + FriendId + " and Friend_ItemId IN (";
            for (int i = 0; i < ItemId.Length; i++)
            {
                if (i < ItemId.Length - 1)
                {
                    CMD += ItemId[i] + ",";
                }
                else
                {
                    CMD += ItemId[i];
                }
            }
            CMD += ")";
            using (SqlCommand Command = new SqlCommand(CMD, DB.DB_CONN))
            {
                DB.DB_CONN.Open();
                if (Command.ExecuteNonQuery() > 0)
                {
                    DB.CloseDB();
                    return true;
                }
                DB.CloseDB();
                return false;
            }
            return false;
        }
    }
}
